package reading.web.config;

import com.fasterxml.classmate.TypeResolver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.http.ResponseEntity;
import org.springframework.web.context.request.async.DeferredResult;
import reading.common.setting.SwaggerAdminSetting;
import reading.common.setting.SwaggerApiSetting;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.WildcardType;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZonedDateTime;
import java.util.Collections;

import static springfox.documentation.schema.AlternateTypeRules.newRule;

/**
 * @author txw
 * Created by txw on 2018-10-12
 * @date 2018/10/12
 */
@Configuration
@EnableSwagger2
@Import({springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration.class})
@AutoConfigureAfter(WebConfiguration.class)
public class SwaggerConfiguration {

    @Autowired
    private SwaggerInapiSetting swaggerInapiSetting;


    @Autowired
    private TypeResolver typeResolver;
    @Bean
    public Docket inapi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .enable(swaggerInapiSetting.isShow())
                .groupName(swaggerInapiSetting.getGroup())
                .apiInfo(new ApiInfo(swaggerInapiSetting.getTitle(), swaggerInapiSetting.getDescription(),
                        swaggerInapiSetting.getVersion(), swaggerInapiSetting.getTermsOfServiceUrl(),
                        new Contact(swaggerInapiSetting.getContact(), "", ""),
                        swaggerInapiSetting.getLicense(), swaggerInapiSetting.getLicenseUrl(), Collections.emptyList()))
                .select()
                .apis(RequestHandlerSelectors.basePackage("reading.web"))
                .paths(PathSelectors.any())
                .build()
                .directModelSubstitute(ZonedDateTime.class, String.class)
                .directModelSubstitute(LocalDateTime.class, String.class)
                .directModelSubstitute(LocalDate.class, String.class)
                .directModelSubstitute(LocalTime.class, String.class)
                .genericModelSubstitutes(ResponseEntity.class)
                .pathMapping("/")
                .alternateTypeRules(
                        newRule(typeResolver.resolve(DeferredResult.class,
                                typeResolver.resolve(ResponseEntity.class, WildcardType.class)),
                                typeResolver.resolve(WildcardType.class)))
                .useDefaultResponseMessages(false)
                .enableUrlTemplating(true);
    }


}
